package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.core.annotation.DataPermission;
import com.tsd.core.constants.Constants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.SqlUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysGroupPerson;
import com.tsd.system.entity.SysGroupPersonExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysGroupPersonService;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：部门员工 控制层
 *
 * @author zsy
 * @date 2020/12/29 15:52:20
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysGroupPersonController extends SystemBaseController {

    @Resource
    private SysGroupPersonService sysGroupPersonService;

    @ApiOperation(value = "根据id获取部门员工")
    @ResponseBody
    @RequestMapping(value = "/getSysGroupPersonBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysGroupPersonBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, CommonParamNameConstants.SID);
            super.checkEmpty(sid, CommonMsgNameConstants.EMPTY_MSG_SID);
            SysGroupPerson sysGroupPerson = sysGroupPersonService.queryBySid(sid);
            hr.setData(sysGroupPerson);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除部门员工")
    @ResponseBody
    @RequestMapping(value = "/deleteSysGroupPerson.action", method = RequestMethod.POST)
    public HttpResult<Object> deleteSysGroupPerson() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get(CommonParamNameConstants.SIDS);
            super.checkEmpty(sids, CommonMsgNameConstants.EMPTY_MSG_SIDS);
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            sysGroupPersonService.deleteSysGroupPerson(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存部门员工")
    @ResponseBody
    @RequestMapping(value = "/saveSysGroupPerson.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysGroupPerson() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String json = JSON.toJSONString(map);
            SysGroupPerson t = JSON.parseObject(json, SysGroupPerson.class);
            sysGroupPersonService.saveSysGroupPerson(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询部门员工")
    @ResponseBody
    @RequestMapping(value = "/findSysGroupPersons.action", method = RequestMethod.POST)
    public HttpResult<List<SysGroupPersonExt>> findSysGroupPersons() {
        HttpResult<List<SysGroupPersonExt>> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyIntegerValue(map, params, CommonParamNameConstants.DR, false, 0);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.GROUP_SID, false, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SEARCH_VALUE, false, true);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SELECTED_VALUE, false, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.ORDER_BY_FIELD, false, false);
            SqlUtil.checkValidOrderBy(HlpUtils.getString(map, CommonParamNameConstants.ORDER_BY_FIELD));
            PageBean page = new PageBean(map).procNoPage(map);
            List<SysGroupPersonExt> list = sysGroupPersonService.findSysGroupPersons(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "批量删除部门员工")
    @ResponseBody
    @RequestMapping(value = "/batchDeleteSysGroupPerson.action", method = RequestMethod.POST)
    public HttpResult<String> batchDeleteSysGroupPerson() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            List<String> sidList = super.getParamsList(map, CommonParamNameConstants.SID_LIST, String.class, true);
            sysGroupPersonService.batchDeleteSysGroupPerson(sidList, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据授权分页查询部门员工")
    @ResponseBody
    @DataPermission(code = "sysGroupTree")
    @RequestMapping(value = "/findSysGroupPersonsByAuth.action", method = RequestMethod.POST)
    public HttpResult<List<SysGroupPersonExt>> findSysGroupPersonsByAuth() {
        HttpResult<List<SysGroupPersonExt>> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = super.getDataPermissionParams();
            HlpUtils.copyIntegerValue(map, params, CommonParamNameConstants.DR, false, 0);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.GROUP_SID, false, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SEARCH_VALUE, false, true);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SELECTED_VALUE, false, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.ORDER_BY_FIELD, false, false);
            params.put(Constants.KEY_SKIP_ADMIN, Constants.VALUE_1);
            SqlUtil.checkValidOrderBy(HlpUtils.getString(map, CommonParamNameConstants.ORDER_BY_FIELD));
            PageBean page = new PageBean(map).procNoPage(map);
            super.filterGroups(map, params);
            String exclude_me = HlpUtils.getString(map, CommonParamNameConstants.EXCLUDE_ME);
            if (!HlpUtils.isEmpty(exclude_me)) {
                params.put(CommonParamNameConstants.EXCLUDE_ME, opUser.getGroup_person_sid());
            }
            List<SysGroupPersonExt> list = sysGroupPersonService.findSysGroupPersons(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
